/**
 * Clase para agrupar opciones como un control 
 */
qx.Class.define("mutual.options", {
	/*
	 * ****************************************************************************
	 * EVENTS
	 * ****************************************************************************
	 */
	events : {
		/**
		 * Se dispara cuando un valor del Radio cambia.
		 */
		"changeValue" : "qx.event.type.Event"
	},
	extend : qx.ui.container.Composite,

	construct : function(label, structure, hAlign) {
		this.base(arguments);

		if (hAlign) {
			this.setLayout(new qx.ui.layout.HBox(5));
		} else {
			this.setLayout(new qx.ui.layout.VBox(5));
		}
		if (label){
			this._label = new qx.ui.basic.Label(label);
			this._label.setWidth(120);
			this._label.setRich(true);
			this.add(this._label);
		}

		this._rdg = new qx.ui.form.RadioGroup;

		this._radios = [];

		for (r in structure) {
			if (structure[r].value != null) {
				if (structure[r].label != null) {
					this._radios
							.push((new qx.ui.form.RadioButton(structure[r].label))
									.set({
												model : structure[r].value
											}));
				} else {
					this._radios
							.push((new qx.ui.form.RadioButton(structure[r].value))
									.set({
												model : structure[r].value
											}));
				}
			} else {
				if (structure[r].label != null) {
					this._radios
							.push((new qx.ui.form.RadioButton(structure[r].label))
									.set({
												model : structure[r].label
											}));
				}
			}
		}

		this._rdg.addListener("changeSelection", function() {
					this.fireEvent('changeValue');
				}, this);

		var r = "";
		for (r in this._radios) {
			// *************** EL 'IF' ES PARCHE PARA INTERNET EXPLORER
			// **********************
			if (typeof this._radios[r] != 'function') {
				this._rdg.add(this._radios[r]);
				this.add(this._radios[r]);
			}
		}

	},

	/*
	 * ****************************************************************************
	 * MEMBERS
	 * ****************************************************************************
	 */
	members : {
		_label : null,
		_radios : null,
		_rdg : null,

		/**
		 * Devuelve el objeto qx.ui.basic.Label.
		 * 
		 * @return {qx.ui.basic.Label}
		 */
		getLabel : function() {
			if (this._label){
				return this._label;
			}
		},

		/**
		 * Devuelve el valor del qx.ui.form.RadioButton seleccionado.
		 * 
		 * @return {String[]}
		 */
		getValue : function() {
			return this._rdg.getSelection()[0].getModel();
		},

		/**
		 * Devuelve el texto del qx.ui.basic.Label del qx.ui.form.RadioButton
		 * seleccionado.
		 * 
		 * @return {String[]}
		 */
		getValueDisplay : function() {
			return this._rdg.getSelected().getLabel();
		},

		/**
		 * Asigna que qx.ui.form.RadioButton del qx.ui.form.ow.Radios sera el
		 * seleccionado.
		 * 
		 * @param value
		 *            {String} Valor del qx.ui.form.RadioButton que queremos
		 *            seleccionar.
		 * @return {String[]}
		 */
		setValue : function(value) {
			this._rdg.setModelSelection([value]);
		},
		focus : function() {
			this._radios[0].focus();
		}
	}
});
